<%@ page import="com.bw.pojo.Car" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: Aaking-aline
  Date: 2022-04-11
  Time: 9:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>信息展示页</title>
    </head>
    <body>
        <center>
            <table>
                <tr>
                    <td><input type="checkbox" id="selectAll"></td>
                    <td>id</td>
                    <td>汽车名称</td>
                    <td>汽车种类</td>
                    <td>汽车价格</td>
                    <td>汽车库存</td>
                    <td>汽车生产日期</td>
                    <td>汽车产地</td>
                    <td>汽车车架号</td>
                    <td>操作</td>
                </tr>

                <%
                    List<Car> list = (List<Car>) request.getAttribute("list");

                    if(list == null){
                        response.sendRedirect("SelectCarByLimit?page=1");
                        return ;
                    }

                    for(Car car:list){ %>
                        <tr>
                            <td><input type="checkbox" class="selectCar" name="<%=car.getId()%>"></td>
                            <td><%=car.getId()%></td>
                            <td><%=car.getCar_name()%></td>
                            <td><%=car.getCar_kind()%></td>
                            <td><%=car.getCar_price()%></td>
                            <td><%=car.getCar_store()%></td>
                            <td><%=car.getCar_make_date()%></td>
                            <td><%=car.getCar_make_addr()%></td>
                            <td><%=car.getCar_number()%></td>
                            <td><input type="button" value="删除" class="deleteBtn"></td>
                        </tr>
                <%  } %>
            </table>

            <input type="button" value="增加新汽车" id="insertCar">
            <input type="button" value="批量删除汽车" id="allDelete">

            <input type="button" value="上一页" id="pre">
            <span>页码:</span>
            <span id="nowPage"><%=request.getAttribute("page")%></span>
            <span>总汽车数:<%=request.getAttribute("number")%></span>
            <input type="button" value="下一页" id="next">

        </center>
    </body>

<%--    增加新汽车功能--%>
    <script>
        document.getElementById("insertCar").onclick = function () {
            window.location.href = "insertCar.jsp";
		}
    </script>

<%--    批量删除汽车功能--%>
    <script>
        document.getElementById("allDelete").onclick = function(){
			let str = "";
			let arr = document.getElementsByClassName("selectCar");

			for(let i = 0;i < arr.length;i++){
				if(arr[i].checked){
					str = str + arr[i].name + ",";
				}
			} //1,4,9,

			str = str.slice(0,str.length-1);  // 1,4,9

			window.location.href = "DeleteServlet?id=" + str;

		}
    </script>

<%--    上一页功能--%>
    <script>
        document.getElementById("pre").onclick = function(){
			let nowPage = document.getElementById("nowPage");
			window.location.href = "SelectCarByLimit?page=" + (parseInt(nowPage.innerText) - 1);
        }
    </script>

<%--    下一页功能--%>
    <script>
		document.getElementById("next").onclick = function(){
			let nowPage = document.getElementById("nowPage");
			window.location.href = "SelectCarByLimit?page=" + (parseInt(nowPage.innerText) + 1);
		}
    </script>



</html>
